
package com.jf.cloud.product.controller.supplier;

import com.jf.cloud.common.constant.IsPassShopEnum;
import com.jf.cloud.common.product.vo.BrandVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.product.dto.BrandDTO;
import com.jf.cloud.product.dto.BrandSigningDTO;
import com.jf.cloud.product.service.BrandService;
import com.jf.cloud.product.service.BrandShopService;
import com.jf.cloud.product.vo.BrandSigningVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zz
 * @Date 2021/4/30 13:26
 */
@RestController("supplierBrandController")
@RequestMapping("/s/apply_supplier/brand")
@Tag(name = "supplier-品牌信息")
public class BrandController {

    @Autowired
    private BrandShopService brandShopService;

    @Autowired
    private BrandService brandService;

    @PostMapping("/signing")
    @Operation(summary = "签约品牌" , description = "签约品牌")
    public ServerResponseEntity<Void> signingBrands(@Valid @RequestBody BrandSigningDTO brandSigningDTO) {
        Long supplierId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        if (Objects.equals(AuthUserContext.get().getIsPassShop(), IsPassShopEnum.YES.value())) {
            brandSigningDTO.setCustomizeBrandList(new ArrayList<>());
        }
        brandShopService.signingBrands(brandSigningDTO, supplierId, sysType);
        return ServerResponseEntity.success();
    }

    @GetMapping("/list_signing")
    @Operation(summary = "获取店铺下已签约的品牌列表" , description = "获取店铺下已签约的品牌列表")
    public ServerResponseEntity<BrandSigningVO> listSigning() {
        Long supplierId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        BrandSigningVO brandSigningVO = brandShopService.listSigningByShopId(supplierId, sysType);
        return ServerResponseEntity.success(brandSigningVO);
    }

    @GetMapping("/list_available_by_category_and_name")
    @Parameters(value = {
            @Parameter(name = "categoryId", description = "分类id" ),
            @Parameter(name = "brandName", description = "品牌名称" )
    })
    @Operation(summary = "根据分类id与品牌名称获取分类下的品牌与店铺签约的品牌" , description = "根据分类id与品牌名称获取分类下的品牌与店铺签约的品牌")
    public ServerResponseEntity<List<BrandVO>> listAvailableBrandByCategoryIdAndBrandName(@RequestParam Long categoryId, @RequestParam(defaultValue = "") String brandName) {
        Long supplierId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        return ServerResponseEntity.success(brandService.listAvailableBrandByCategoryIdAndBrandNameAndShopId(categoryId, brandName, supplierId, sysType));
    }
    @DeleteMapping("/delete_signing_brand")
    @Operation(summary = "根据签约信息Id删除签约品牌" , description = "根据签约信息Id删除签约品牌")
    public ServerResponseEntity<Void> deleteSigningCategory(@RequestParam("brandShopId") Long brandShopId) {
        brandShopService.deleteSigningBrand(brandShopId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/list_apply_signing_brand")
    @Operation(summary = "获取可以签约的品牌列表（已经签约的品牌不会返回）" , description = "获取可以签约的品牌列表（已经签约的品牌不会返回）")
    public ServerResponseEntity<List<BrandVO>> listApplySigningBrand(BrandDTO brandDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        return ServerResponseEntity.success(brandService.listApplySigningBrand(brandDTO, shopId, sysType));
    }
    @PostMapping("/add_signing_brand")
    @Operation(summary = "新增签约品牌" , description = "新增签约品牌")
    public ServerResponseEntity<Void> addSigningBrand(@Valid @RequestBody BrandSigningDTO brandSigningDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        brandShopService.addSigningBrand(brandSigningDTO, shopId, sysType);
        return ServerResponseEntity.success();
    }
}
